﻿using CNFM_CROCODILE_LOGO.AOP.OAuth;
using CNFM_CROCODILE_LOGO.Core.Domain.Current;
using CNFM_CROCODILE_LOGO.Core.Domain.Entity;
using CNFM_CROCODILE_LOGO.Core.Domain.ViewModels;
using CNFM_CROCODILE_LOGO.Core.Repository;
using CNFM_CROCODILE_LOGO.OAuth.OAuth;
using CNFM_CROCODILE_LOGO.RestClient.Utils;
using CNFM_CROCODILE_LOGO.Services.Interfaces;
using Microsoft.IdentityModel.Tokens;
using System;
using System.Collections.Generic;
using System.IdentityModel.Tokens.Jwt;
using System.Linq;
using System.Security.Claims;
using System.Text;

namespace CNFM_CROCODILE_LOGO.Services.Implementations
{
    public class UserServices : IUserServices
    {
        private IEfRepository _rep;

        public UserServices(IEfRepository rep)
        {
            this._rep = rep;
        }

        /// <summary>
        /// 用户登陆
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public ReturnValueModel Login(UserModels model)
        {
            if (string.IsNullOrEmpty(model.LoginName))
                return new ReturnValueModel { Code = 400, Msg = "用户名,不能为空!" };
            if (string.IsNullOrEmpty(model.Password))
                return new ReturnValueModel { Code = 400, Msg = "密码,不能为空!" };

            var userAll = _rep.Where<CrocodileUser>(p => p.Status == 0 && p.LoginName == model.LoginName && p.Password == UtilsHelp.GetMD5(model.Password)).ToList();
            if (userAll.Count() == 0)
                return new ReturnValueModel { Code = 404, Msg = "用户不存在!" };

            var userItem = userAll.FirstOrDefault();
            var roleName = string.Empty;
            if (!string.IsNullOrEmpty(userItem.RoleId))
                roleName = _rep.FirstOrDefault<Role>(p => p.Id == userItem.RoleId).RoleName;
            var enpId = string.Empty;


            var token = JwtHelp.JwtToekn(userItem, roleName);

            TokenModel tokenModel = new TokenModel()
            {
                AccessToken = token,
                UserId = userItem.Id,
                UserName = userItem.UserName,
                RoleId = userItem.RoleId,
                RoleArea = userItem.UnCode,
                RoleName = roleName
            };

            return new ReturnValueModel
            {
                Code = 200,
                Msg = "success!",
                Result = new
                {
                    Data = tokenModel
                }
            };
        }

        /// <summary>
        /// 用户菜单
        /// </summary>
        /// <param name="roleId"></param>
        /// <returns></returns>
        public ReturnValueModel UserMenu(string roleId)
        {
            var userMenu = from a in _rep.Where<Role>(p => p.Status == 0 && p.Id == roleId)
                           join r in _rep.Where<RoleMenu>(p => p.Status == 0) on a.Id equals r.RoleId
                           join m in _rep.Where<Menu>(p => p.Status == 0) on r.MenuId equals m.Id
                           select new
                           {
                               m.MuCode
                           };

            return new ReturnValueModel { Code = 200, Msg = "success!", Result = new { Data = userMenu.ToList() } };
        }
    }
}
